Page({
  data: {
    bookId: '',
    book: {}
  },

  onLoad(options) {
    const bookId = options.id || '';
    const book = this.getBookById(bookId);

    this.setData({
      bookId,
      book
    });

    if (book.pdfUrl) {
      this.openPdf(book.pdfUrl);
    } else {
      wx.showToast({
        title: '找不到PDF文件',
        icon: 'none'
      });
    }
  },

  openPdf(pdfUrl) {
    wx.showLoading({
      title: '正在打开绘本...',
      mask: true
    });

    wx.downloadFile({
      url: pdfUrl,
      success: (res) => {
        const filePath = res.tempFilePath;
        wx.openDocument({
          filePath: filePath,
          fileType: 'pdf',
          showMenu: true,
          success: () => {
            wx.hideLoading();
            // 1~1.5秒后自动返回详情页（等待用户关闭PDF）
          setTimeout(() => {
            wx.navigateBack();
          }, 150);
          },
          fail: () => {
            wx.hideLoading();
            wx.showToast({
              title: '打开失败',
              icon: 'none'
            });
            // 返回详情页
          setTimeout(() => {
            wx.navigateBack();
          }, 100);
          }
        });
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '下载失败',
          icon: 'none'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 100);
      }
    });
  },

  getBookById(id) {
    // 这里是模拟数据，替换成你的真实数据库
    const books = [
      {
        id: 'book1',
        title: '第一个吻',
        pdfUrl: 'https://english-pdf-share-n-1320222167.cos.ap-beijing.myqcloud.com/Alphabet%20A-Z%20Worksheets%20Coloring%20and%20Tracing%20English%20Created%20Resources_compressed.pdf'
      },
      {
        id: 'book2',
        title: '我不想离开你',
        pdfUrl: 'https://english-pdf-share-n-1320222167.cos.ap-beijing.myqcloud.com/Alphabet%20Coloring%20Book%20English%20Created%20Resources_compressed.pdf'
      },
      {
        id: 'book3',
        title: '魔法亲亲',
        pdfUrl: 'https://english-pdf-share-n-1320222167.cos.ap-beijing.myqcloud.com/Alphabet%20Coloring%20and%20Tracing%20Activity%20Book%20Copyright%202022%20English%20Created%20Resources_compressed.pdf'
      },
      {
        id: 'book4',
        title: '国王爸爸',
        pdfUrl: 'https://english-pdf-share-n-1320222167.cos.ap-beijing.myqcloud.com/Alphabet%20Coloring%20and%20Tracing%20Activity%20Book%20Copyright%202022%20English%20Created%20Resources_compressed.pdf'
      },
      {
        id: 'book5',
        title: '我爸爸',
        pdfUrl: 'https://english-pdf-share-n-1320222167.cos.ap-beijing.myqcloud.com/Alphabet%20Coloring%20and%20Tracing%20Activity%20Book%20Copyright%202022%20English%20Created%20Resources_compressed.pdf'
      },
      {
        id: 'book6',
        title: '爸爸带我看世界',
        pdfUrl: 'https://english-pdf-share-n-1320222167.cos.ap-beijing.myqcloud.com/Alphabet%20Coloring%20and%20Tracing%20Activity%20Book%20Copyright%202022%20English%20Created%20Resources_compressed.pdf'
      }
    ];

    return books.find(item => item.id === id) || {};
  }
});
